// pages/weizhangbox/weizhangcaradd/weizhangcaradd.js
import {
  addCar
} from '../../../utils/networkRequest.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ChePaiHao: "",
    CheJiaHao: "",
    FaDongJiHao: "",
    isKeyboard: false, //是否显示键盘
    specialBtn: false,
    tapNum: false, //数字键盘是否可以点击
    parkingData: false, //是否展示剩余车位按钮
    isFocus: false, //输入框聚焦
    isFocusP: false, //手机输入框聚焦
    isFocusV: false, //验证码输入框聚焦
    isFocusN: false, //姓名输入框聚焦
    flag: false, //防止多次点击的阀门 
    keyboardNumber: '1234567890',
    keyboardAlph: 'QWERTYU OPASDFGHJKLZXCVBNM巛',
    keyboard1: '京津沪冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤川青藏琼宁渝',
    keyboard2: '',
    keyboard2For: ['完成'],
    keyboardValue: '',
    textArr: [],
    animationData: {},
    textValue: '', //车牌号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  
  /**
   * 车架号接收
   */
  bindCheJiaHaoInput: function(e) {
    this.setData({
      CheJiaHao: e.detail.detail.value
    })
  },
  /**
   * 发动机号接收
   */
  bindFaDongJiHaoInput: function(e) {
    this.setData({
      FaDongJiHao: e.detail.detail.value
    })
  },
  //保存并查询
  onSubmit: function() {
    let {
      textValue,
      CheJiaHao,
      FaDongJiHao
    } = this.data;
    if (textValue == "") {
      wx.showToast({
        icon: "none",
        title: "请输入车牌号",
      })
    } else if (CheJiaHao == "") {
      wx.showToast({
        icon: "none",
        title: "请输入车架号后六位",
      })
    } else if (FaDongJiHao == "") {
      wx.showToast({
        icon: "none",
        title: "请输入发动机号后六位",
      })
    } else {
      addCar(textValue, CheJiaHao, FaDongJiHao, (r) => {
        console.log(r)
        if (r.code == 0) {
          wx.navigateBack();
        } else {
          wx.showToast({
            icon: "none",
            title: r.message,
          })
        }
      })
    }



  },
  /**
   * 输入框显示键盘状态
   */
  showKeyboard: function() {
    var self = this;
    self.setData({
      isFocus: true,
      isKeyboard: true,
    })
  },



  /**
   * 点击页面隐藏键盘事件
   */
  hideKeyboard: function() {
    var self = this;
    if (self.data.isKeyboard) {
      //说明键盘是显示的，再次点击要隐藏键盘
      if (self.data.textValue) {
        self.setData({
          isKeyboard: false
        })
      } else {
        self.setData({
          isKeyboard: false,
          isFocus: false
        })
      }

    }
  },
  /**
   * 输入框聚焦触发，显示键盘
   */
  bindFocus: function() {
    var self = this;
    if (self.data.isKeyboard) {
      //说明键盘是显示的，再次点击要隐藏键盘
      self.setData({
        isKeyboard: false,
        isFocus: true,
      })
    } else {
      //说明键盘是隐藏的，再次点击显示键盘
      self.setData({
        isFocus: true,
        isKeyboard: true,
      })
    }
  },
  /**
   * 键盘事件
   */
  tapKeyboard: function(e) {
    var self = this;
    //获取键盘点击的内容，并将内容赋值到textarea框中
    var tapIndex = e.target.dataset.index;
    var tapVal = e.target.dataset.val;
    var keyboardValue;
    var specialBtn;
    var tapNum;
    if (tapVal == "巛") {
      //说明是删除 
      self.data.textArr.pop();
      if (self.data.textArr.length == 0) {
        //说明没有数据了，返回到省份选择键盘
        this.specialBtn = false;
        this.tapNum = false;
        this.keyboardValue = self.data.keyboard1;
      } else if (self.data.textArr.length == 1) {
        //只能输入字母
        this.tapNum = false;
        this.specialBtn = true;
        this.keyboardValue = self.data.keyboard2;
      } else {
        this.specialBtn = true;
        this.tapNum = true;
        this.keyboardValue = self.data.keyboard2;
      }
      self.data.textValue = self.data.textArr.join("");
      self.setData({
        textValue: self.data.textValue,
        keyboardValue: this.keyboardValue,
        specialBtn: this.specialBtn,
        tapNum: this.tapNum,
      })
      return false
    }
    if (self.data.textArr.length >= 8) {
      return false;
    }
    self.data.textArr.push(tapVal);
    self.data.textValue = self.data.textArr.join("");
    self.setData({
      textValue: self.data.textValue,
      keyboardValue: self.data.keyboard2,
      specialBtn: true,
    })
    if (self.data.textArr.length > 1) {
      //展示数字键盘
      self.setData({
        tapNum: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    var self = this;
    //将keyboard1和keyboard2中的所有字符串拆分成一个一个字组成的数组
    self.data.keyboard1 = self.data.keyboard1.split('')
    self.data.keyboard2 = self.data.keyboard2.split('')
    self.setData({
      keyboardValue: self.data.keyboard1
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */


  onShow: function() {
    var self = this;
    self.setData({
      flag: false
    })
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease',
    })
    setInterval(function() {
      if (self.data.opacity == 1) {
        animation.opacity(0).step()
        this.setData({
          animationData: animation.export(),
          opacity: 0,
        })
      } else {
        animation.opacity(1).step()
        this.setData({
          animationData: animation.export(),
          opacity: 1,
        })
      }
    }.bind(this), 600)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.stopPullDownRefresh()
  }
})